/****************************************************************************************************/
/**
\file       OS_TaskM.c
\brief      OS task module 
\date       08/03/2014
*/
/****************************************************************************************************/

 #include "OS_TaskM.h"
 #include "OS_Task.h"
 
taskQueueType taskQueue[CNF_MAX_PRIORITY+1];

/**************************************************************************************
* - Function      ActivateTask
* - Description   Service used to activate a task
* - Author        Equipo 1
* - Param         taskID to be activated 
* - Return        OS_StatusType
                        E_OK Task could be activated correctly
                        E_OS_ID  Error caused by invalid task id or priority
****************************************************************************************/
OS_StatusType ActivateTask (TaskType taskID) 
{   
    u8 taskPriority;
    volatile u8 currentQueueIndex;
    OS_StatusType returnStatus = E_OS_ID; 
    taskPriority = schm_task_status[taskID].priority;
    /*Validata that current priority is valid*/
    if(taskPriority <= CNF_MAX_PRIORITY ) 
    {
        /*Validata task id is valid*/
       if(taskID < Task_total) 
       {
            /*Get curren index in task queue*/
            currentQueueIndex = taskQueue[taskPriority].queueIndex;
           /*Set task as active*/
            schm_task_status[taskID].taskState = TASK_READY;
            taskQueue[taskPriority].taskQueue[currentQueueIndex].id = taskID;
            /*Queue task in the corresponding queue*/

            taskQueue[taskPriority].taskQueue[currentQueueIndex].task_function = schm_task_status[taskID].taskfunction;
       
            taskQueue[taskPriority].queueIndex++;
            
            if(taskQueue[taskPriority].queueIndex == CNF_TASK_QUEUE_DEEP_PER_PRIORITY ) 
            {
              taskQueue[taskPriority].queueIndex=0;
            }
            returnStatus= E_OK; 
       } 
    }
    return returnStatus;
}

/**************************************************************************************
* - Function      TerminateTask
* - Description   Service used to terminate a task
* - Author        Equipo 1
* - Param         taskID to be terminated 
* - Return        OS_StatusType
                        E_OK Task could be activated correctly
                        E_OS_RESOURCE  Error caused by invalid task id
****************************************************************************************/
OS_StatusType TerminateTask(void )
{  
  OS_StatusType returnStatus = E_OS_RESOURCE;  
  /*Task is valid*/
  if(currentRunning<Task_total) 
  {
       /*Set task as suspended*/
      schm_task_status[currentRunning].taskState = TASK_SUSPENDED;
      currentRunning = INVALID_TASK; 
      returnStatus= E_OK; 
  }
  return returnStatus;
    
}

/**************************************************************************************
* - Function      GetTaskID
* - Description   Service used get task ID that is it running currently
* - Author        Equipo 1
* - Param         taskIDRef reference where active taskID is returned
* - Return        OS_StatusType
                        E_OK Task could be activated correctly
                        E_OS_RESOURCE  Error caused by NULL pointer passed as taskIDRef
****************************************************************************************/
OS_StatusType GetTaskID(TaskRefType taskIDRef)
{
  OS_StatusType returnStatus = E_OS_RESOURCE;
  /*taskIDRef is not null*/
  if(NULL != taskIDRef) 
  {
      *taskIDRef = currentRunning; 
      returnStatus = E_OK;
  } 
  return returnStatus;

}

/**************************************************************************************
* - Function      GetTaskState
* - Description   Service used to terminate a task
* - Author        Equipo 1
* - Param         taskID to get its current state
* - Param         stateRef reference where task state is returned
* - Return        OS_StatusType
                        E_OK Task could be activated correctly
                        E_OS_ID  Error caused by invalid task id or null pointer as stateRef
****************************************************************************************/
OS_StatusType GetTaskState(TaskType taskID, TaskStateRefType stateRef)
{
  OS_StatusType returnStatus = E_OS_ID;
  if(taskID<Task_total) 
  {
      if(NULL != stateRef) 
      {
        *stateRef = schm_task_status[taskID].taskState;
        returnStatus = E_OK;
      }
  }
  return returnStatus; 
}







